<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Proposal_model extends CI_Model
{
	
	public function __construct()
	{
		parent::__construct();

	}
	public function  create_module($insert)
	{
		$this->db->insert('proposal_module',$insert);
	}
	public function  create_proposal($insert)
	{
		$this->db->insert('proposal_name',$insert);
	}
	public function create_categories($insert)
	{
	$this->db->insert('proposal_category',$insert);
	}
	public function create_section($insert)
	{
	$this->db->insert('proposal_section',$insert);
	}
	public function create_feature($insert)
	{
		//feature
		$this->db->insert('proposal_feature',$insert);
	}
	
	public function get_all_modules()
	{
		return $this->db->get('proposal_module')->result();
	} 
	public function get_all_proposal()
	{
		return $this->db->get('proposal_name')->result();
	}
	public function get_all_category()
	{
		return $this->db->get('proposal_category')->result();
	}
	public function get_all_section()
	{
		return $this->db->get('proposal_section')->result();
	}
	public function getcategoryid($id)
	{
	 $section=$this->db->query ("SELECT category_id\n"
    . "FROM proposal_name\n"
    . "where proposal_name.id='$id'");
	 return $section->result();
	
	}
	public function get_section($category)
	{
		return $this->db->get_where('proposal_section',array('category_id' => $category))->result();
	}
	public function getsectionbody($category)
	{
		return $this->db->get_where('proposal_section',array('category_id' => $category,'position'=>'body'))->result();
	}
	public function getsectionfooter($category)
	{
		return $this->db->get_where('proposal_section',array('category_id' => $category,'position'=>'footer'))->result();
	}
	public function get_module($id)
	{
	 $section=$this->db->query ("SELECT proposal_module.id , proposal_module.name 
          FROM proposal_category JOIN proposal_section ON proposal_category.id =proposal_section.category_id
        JOIN proposal_module ON proposal_section.id = proposal_module.section_id where proposal_category.id='$id'");
		return  $section->result();
          
	}
	public function get_module_id($sid)
	{
	return $this->db->get_where('proposal_module',array('section_id' => '$sid'))->result();
	}
	public function getallmodule($id)
	{
	
	 $section=$this->db->query ("SELECT proposal_module.id , proposal_module.name,proposal_module.description,proposal_module.price 
          FROM proposal_category JOIN proposal_section ON proposal_category.id =proposal_section.category_id
        JOIN proposal_module ON proposal_section.id = proposal_module.section_id where proposal_category.id='$id'");
		return  $section->result();
	}
	
	public function get_features_by_module($module_id)
	{
		return $this->db->get_where('proposal_feature',array('module_id' => $module_id))->result();
	}
	public function get_previewmodule($section_id)
	{
	return $this->db->get_where('proposal_module',array('section_id' => $section_id))->result();
	}
	
	public function get_previewfeatures($module_id)
	{
	return $this->db->get_where('proposal_feature',array('module_id' => $module_id))->result();
	
	}
	
	public function getproposaldetail($id)
	{
	return $this->db->get_where('proposal_name',array('id' => $id))->result();
	
	}
	public function get_previewfooter($module_id)
	{
	return $this->db->get_where('proposal_module',array('section_id' => $module_id))->result();
	
	}
	public function delete_module($id)
	{
		$this->db->where('id', $id);
        $this->db->delete('proposal_module'); 
	}
	public function delete_feature($id)
	{
		$this->db->where('id', $id);
        $this->db->delete('proposal_feature'); 
	}
	public function delete_category($id)
	{
		 $section=$this->db->query("
		Delete t1, t2, t3,t4 From proposal_category as t1 
        INNER JOIN  proposal_section as t2 on t1.id = t2.category_id
        INNER JOIN  proposal_module as t3 on t2.id=t3.section_id
		INNER JOIN proposal_feature as t4 on t3.id=t4.module_id
        WHERE  t1.id='$id'");
		
		$this->db->where('id', $id);
        $this->db->delete('proposal_category'); 
	}
	public function delete_proposal($id)
	{
		$this->db->where('id', $id);
        $this->db->delete('proposal_name'); 
	}
	public function update_module($id,$name,$description,$price)
		{
		$data= array(
			'name' =>$name ,
			'price' =>$price,	
             'description'=>$description	);
	    $this->db->where('id',$id);
	    $re= $this->db->update('proposal_module',$data);  
		}
	public function update_feature($id,$name,$description,$price)
		{
		$data= array(
			'name' =>$name ,
			'price' =>$price,	
             'description'=>$description	);
	    $this->db->where('id',$id);
	    $re= $this->db->update('proposal_feature',$data);  
		}
}
